/*
********************************************************************************
Description:	This code conducts a balancing test to test whether
				the treatment and control groups looks balanced using the "could
				have been selected for random enquiry" sample created in the do-
				file CVS_constructCouldHaveAnalysisDataset.
				
Typical usage:  run "$dofiles\CVS_couldHaveBalancingTest.do"
				runBalancingTests using "$mergedata\CouldhaveSample.dta"

********************************************************************************
*/


**********************************
* PROGRAM TO RUN BALANCING TESTS *
**********************************

capture program drop runBalancingTests
program define runBalancingTests
	
	syntax name(name=VarVersion) [using/] [, saving(string) trimlevel(real 0)] ctrlsurvives(real)

	* Open the data
	if ("`using'" != "") use "`using'", clear
	
	* Open the file if saving to a file
	if ("`saving'" != "") {
		local balancingFile "balancingFile"
		capture file close `balancingFile'
		file open `balancingFile' using "`saving'", write replace 
	}
	
	* List variables we want to check balance of (for use later)
	global balancingvars "female age inLonSE agent_in survives taxableinccg_`VarVersion' it_cl4_cgt_`VarVersion' empinc_`VarVersion' sempinc_`VarVersion' intdivinc_`VarVersion' pensinc_`VarVersion' propinc_`VarVersion' "


	***************
	* Balance tests
	***************

	if ("`saving'" != "") file write `balancingFile' "Balance tests" _n

	* Balancing tests for nonfinancial covariates
	if (`ctrlsurvives'==0) drawBalancingTable female age inLonSE agent_in survives, fmt(%12.3f) balancingFile(`balancingFile') printheader trimlevel(`trimlevel') ctrlsurvives(`ctrlsurvives') diff(`ctrlsurvives')
	else drawBalancingTable female age inLonSE agent_in, fmt(%12.3f) balancingFile(`balancingFile') printheader trimlevel(`trimlevel') ctrlsurvives(`ctrlsurvives') diff(`ctrlsurvives')
	di

	* Balancing tests for SA302 income and tax totals
	drawBalancingTable taxableinccg_`VarVersion' it_cl4_cgt_`VarVersion', fmt(%12.0f) balancingFile(`balancingFile') trimlevel(`trimlevel') ctrlsurvives(`ctrlsurvives') diff(`ctrlsurvives')
	di

	* Balancing tests for SA302 income components
	drawBalancingTable empinc_`VarVersion' sempinc_`VarVersion' pensinc_`VarVersion' propinc_`VarVersion' intdivinc_`VarVersion' intinc_`VarVersion' divinc_`VarVersion', fmt(%12.0f) balancingFile(`balancingFile') trimlevel(`trimlevel') ctrlsurvives(`ctrlsurvives') diff(`ctrlsurvives')
	di
	
	if ("`saving'" != "") file close `balancingFile'

	
end



*********************************************************************************
* PROGRAM TO DRAW A TABLE OF BALANCING TEST RESULTS FOR A GIVEN SET OF OUTCOMES *
*********************************************************************************

capture program drop drawBalancingTable
program define drawBalancingTable

	syntax varlist [if/] [, fmt(string) balancingFile(name) trimlevel(real 0) printheader ctrlsurvives(integer 0) diff(integer 0)]

	if "`fmt'" == "" local fmt "%12.2f"
	if "`if'" == "" local if "1"
	
	if ("`printheader'" == "printheader") {
		di as text %36s "" %12s "-5" %12s "-4" %12s "-3" %12s "-2" %12s "-1"  %12s "0"
		if ("`balancingFile'" != "") file write `balancingFile' ",yrssince,-5,-4,-3,-2,-1,0" _n
	}
		
	* Create local to list pre-audit survival variables
	if (`ctrlsurvives' == 0) local ctrlsurvives  ""
	else {
		* Create the pre-audit survives variables (copied across all yrssince)
		foreach pre of numlist 1(1)`ctrlsurvives' {
			di as text "pre`pre', " _c
			egen byte survives_pre`pre' = total(survives*(yrssince == -`pre')), by(utr_no audityear)
		}
	local ctrlsurvives "survives_pre1-survives_pre`ctrlsurvives'"
	}
	
	foreach var of local varlist {
	
		local ctrltxt `"%24s "`var'" %12s "Mean""'
		local difftxt `"%24s "" %12s "Diff""'
		local pvaltxt `"%24s "" %12s "p-value""'
		local Ntxt `"%24s "" %12s "N""'

		local ctrltxtfile `""`var',Mean""'
		local difftxtfile `"",Diff""'
		local pvaltxtfile `"",p-value""'
		local Ntxtfile `"",N""'

		* Create trimmed variable
		if (`trimlevel' > 0) & ("`var'"!="survives") {
			qui centile `var' if `if' & (`var' > 0 & `var' < .), c(`=100 - `trimlevel'')
			egen byte trimmed = max(`var' > `r(c_1)'), by(utr_no audityear)
		}
		else gen byte trimmed = 0
		
		* Set up differencing if requested
		if (`diff' > 0) {
			* Create differenced outcome variable
			egen double `var'_avg`diff' = total(`var'*inrange(yrssince,-`diff',-1)/`diff'), by(utr_no audityear)
			qui replace `var'_avg`diff' = . if minyrssince > -`diff'
			cap drop `var'_diff`diff'
			qui gen double `var'_diff`diff' = `var' - `var'_avg`diff'
			drop `var'_avg`diff'
			local outcomename "`var'_diff`diff'"
		}
		else local outcomename "`var'"
		
		
		foreach horizon of numlist -5(1)0 {
		
			qui reg `outcomename' treatment i.tax_year `ctrlsurvives' if yrssince==`horizon' & trimmed == 0 & `if', cluster(utr_no)
			local ctrltxt `"`ctrltxt' `fmt' `=_b[_cons]'"'
			local difftxt `"`difftxt' `fmt' `=_b[treatment]'"'
			local ctrltxtfile `"`ctrltxtfile' "," `fmt' (`=_b[_cons]')"'
			local difftxtfile `"`difftxtfile' "," `fmt' (`=_b[treatment]')"'
			
			qui reg `outcomename' i.tax_year treatment#tax_year `ctrlsurvives' if yrssince==`horizon' & trimmed == 0 & `if', cluster(utr_no)
			qui testparm treatment#tax_year
			local pvaltxt `"`pvaltxt' %12.3f `r(p)'"' 
			local Ntxt `"`Ntxt' %12.0g `e(N)'"' 
			local pvaltxtfile `"`pvaltxtfile' "," %12.3f (`r(p)')"'
			local Ntxtfile `"`Ntxtfile' "," %12.0g (`e(N)')"'
			
		}
		
		di `ctrltxt'
		di `difftxt'
		di `pvaltxt'
		di `Ntxt'
		
		if ("`balancingFile'" != "") {
			file write `balancingFile' `ctrltxtfile' _n
			file write `balancingFile' `difftxtfile' _n
			file write `balancingFile' `pvaltxtfile' _n
			file write `balancingFile' `Ntxtfile' _n
		}
		
		drop trimmed
		
	}

end


exit

